/**
 * Created by Administrator on 2017/6/17.
 */

let userDao = require('../mysql/userDao.js');
let weixinSDK = require('../util/weixinSDK.js');
let log = require('../util/log.js');
let create = require('./create.js');
let rpc = require('../util/rpc.js');

module.exports = weixin;

function weixin(token, expire, code, platform ) { 
    this._token = token;
    this._expire = expire;
    this._platform = platform;
    this._code = code; 
    this._refresh_token = null; 
    this._uid = null;
    this._name = null;
    this._avatar = null;
    this._cb = null;
}

weixin.prototype.run = function (cb) {
    this._cb = cb;
    if (this._expire <= 0) {
        this.refresh();
    } else {
        this.self();
    }   
};

weixin.prototype.refresh = function () {
    //console.log('_code:' + this._code + '过期，尝试更换');
    var _this = this;
    weixinSDK.refresh(this._code, function (result) { 
        if (result.code && result.code!=0) { 
            log.e('weixin refresh error:' + result.message);
            _this._cb(null, {'error': 103, 'msg':'登陆失败> '+result.code+','+result.message});
            return false;
        }   
        //console.log(result);
        _this._token = result.access_token;
        _this._expire = result.expires_in;
        _this._openid = result.openid; 
        _this._refresh_token = result.refresh_token;
        _this.self();
    }); 
};  

weixin.prototype.self = function () {
    //console.log('access_token:' + this._token + '开始拉取用户信息'+this._openid);
    let _this = this;
    weixinSDK.self(this._token, this._openid, function (result) {
        if (result.code && result.code!=0) { 
            log.e('weixin self error:' + result.message);
            _this._cb(null, {'error': 103, 'msg':'登陆失败> '+result.code+','+result.message});
            return false;
        }   
        //console.log(result);
        _this._uid = result.openid;
        _this._name = result.nickname;
        _this._avatar = result.headimgurl;
        let userModel = new userDao();
        userModel.inWeixinId(_this._uid, function (err, res) {
            if (res == null) {
                _this.reg();
            } else {
                console.log(_this._avatar);
                let param = {};
                param.name = _this._name;
                param.avatar = _this._avatar;
                create.reToken(userModel, param, _this._cb);
            }
        }); 
        let param = {};
        param.uid = _this._uid;
        param.accessToken = _this._token;
        param.expire = _this._expire;
        param.platform = _this._platform; 
        param.refreshToken = _this._refresh_token;
        rpc.dc.send('setWeixin',param);
    }); 
};  

weixin.prototype.reg = function () {
    let _this = this;
    create.weixin(this._uid, this._name, this._avatar, function (err, res,token) {
        if (err!=null || res==null) {
            _this._cb(null, {'error': 105, 'msg': '创建账号失败'});
            return false;
        }   
        _this._cb(null, {'token': token });
    });
};